// pages/todo/todo.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        keyword:"",
        todoList:[],
        doneList:[],
        todoLength:0,
        doneLength:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var {todoList,doneList} = this.data
        var todo = wx.getStorageSync('todo')
        var done = wx.getStorageSync('done')
        todoList = JSON.parse(todo)
        doneList = JSON.parse(done)
        this.setData({todoList,doneList})
    },  
    handleBlur(event){
        var {todoList} = this.data
        var value = event.detail.value
        todoList.push({value,done:false})
        this.setData({todoList,keyword:""})
        wx.setStorageSync('todo', JSON.stringify(todoList))
    },
    handleDelete(event){
        var {aid} = event.currentTarget.dataset
        var {todoList} = this.data
        todoList = todoList.filter(item=>{
            return item.value !== aid
        })
        this.setData({todoList})
        wx.setStorageSync('todo', JSON.stringify(todoList))
    },
    handleSelect(event){
        var {aid} = event.currentTarget.dataset
        var {todoList,doneList} = this.data
        todoList = todoList.filter(item=>{
            if(item.value == aid){
                item.done = !item.done
                doneList.push(item)
            }
            return item.done == false
        })
        this.setData({todoList,doneList})
        wx.setStorageSync('todo', JSON.stringify(todoList))
        wx.setStorageSync('done', JSON.stringify(doneList))
    },
    doneSelect(event){
        var {aid} = event.currentTarget.dataset
        var {todoList,doneList} = this.data
        doneList = doneList.filter(item=>{
            if(item.value == aid){
                item.done = !item.done
                todoList.push(item)
            }
            return item.done == true
        })
        this.setData({doneList,todoList})
        wx.setStorageSync('todo', JSON.stringify(todoList))
        wx.setStorageSync('done', JSON.stringify(doneList))
    },
    doneDelete(event){
        var {aid} = event.currentTarget.dataset
        var doneList = this.data.doneList
        doneList = doneList.filter(item=>{
            return item.value !== aid
        })
        this.setData({doneList})
        wx.setStorageSync('done', JSON.stringify(doneList))
    }
})